<template>
  <div class="col-xs-12 col-md-8 offset-md-2">
    <form class="card comment-form" @submit.prevent="(_) => false">
      <div class="card-block">
        <textarea
          class="form-control"
          placeholder="Write a comment..."
          rows="3"
          v-model="comment.body"
        ></textarea>
      </div>
      <div class="card-footer">
        <img :src="user.image" class="comment-author-img" />
        <button class="btn btn-sm btn-primary" @click="postComment">
          Post Comment
        </button>
      </div>
    </form>

    <div class="card" v-for="comment in comments" :key="comment.id">
      <div class="card-block">
        <p class="card-text">
          {{ comment.body }}
        </p>
      </div>
      <div class="card-footer">
        <!-- <a href="" class="comment-author"> -->
        <nuxt-link
          class="comment-author"
          :to="{
            name: 'profile',
            params: { username: comment.author.username },
          }"
        >
          <img :src="comment.author.image" class="comment-author-img" />
        </nuxt-link>
        &nbsp;
        <nuxt-link
          class="comment-author"
          :to="{
            name: 'profile',
            params: { username: comment.author.username },
          }"
        >
          {{ comment.author.username }}
        </nuxt-link>
        <span class="date-posted">{{
          article.createdAt | date("MMM DD, YYYY")
        }}</span>
        <span class="mod-options">
          <i class="ion-edit"></i>
          <i class="ion-trash-a"></i>
        </span>
      </div>
    </div>

    <!-- <div class="card">
      <div class="card-block">
        <p class="card-text">
          With supporting text below as a natural lead-in to additional content.
        </p>
      </div>
      <div class="card-footer">
        <a href="" class="comment-author">
          <img
            src="http://i.imgur.com/Qr71crq.jpg"
            class="comment-author-img"
          />
        </a>
        &nbsp;
        <a href="" class="comment-author">Jacob Schmidt</a>
        <span class="date-posted">Dec 29th</span>
        <span class="mod-options">
          <i class="ion-edit"></i>
          <i class="ion-trash-a"></i>
        </span>
      </div>
    </div> -->
  </div>
</template>

<script>
import { getComments, addComments } from "@/api/article";
import { mapState } from "vuex";

const { log } = console;
export default {
  name: "ArticleComments",
  props: ["article"],
  data() {
    return {
      comments: [],
      comment: {
        body: "",
      },
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  mounted() {
    this.getComments();
  },
  methods: {
    async getComments() {
      const { data } = await getComments(this.article.slug);
      this.comments = data.comments;
    },
    async postComment() {
      const { data } = await addComments(this.article.slug, {
        comment: this.comment,
      });
      this.getComments();
      this.comment.body = "";
    },
  },
};
</script>

<style>
</style>